home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / previe_1 / previewp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  9.5 KB  |  370 lines

  1. // PreviewPrintView.cpp : implementation of the CPreviewPrintView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "PreviewPrint.h"
  6.  
  7. #include "PreviewPrintDoc.h"
  8. #include "PreviewPrintView.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CPreviewPrintView
  18.  
  19. IMPLEMENT_DYNCREATE(CPreviewPrintView, CFormView)
  20.  
  21. BEGIN_MESSAGE_MAP(CPreviewPrintView, CFormView)
  22.     //{{AFX_MSG_MAP(CPreviewPrintView)
  23.     ON_BN_CLICKED(IDC_BTN_BEGINDOC, OnBtnBegindoc)
  24.     ON_BN_CLICKED(IDC_BTN_ENDDOC, OnBtnEnddoc)
  25.     ON_BN_CLICKED(IDC_BTN_GETPOS, OnBtnGetpos)
  26.     ON_BN_CLICKED(IDC_BTN_MOVETO, OnBtnMoveto)
  27.     ON_BN_CLICKED(IDC_BTN_LINETO, OnBtnLineto)
  28.     ON_BN_CLICKED(IDC_BTN_TEXTOUT, OnBtnTextout)
  29.     ON_BN_CLICKED(IDC_BTN_FONT, OnBtnFont)
  30.     ON_BN_CLICKED(IDC_BTN_GETTEXTALIGN, OnBtnGettextalign)
  31.     ON_CBN_SELCHANGE(IDC_CBO_TEXTALIGN, OnSelchangeCboTextalign)
  32.     ON_BN_CLICKED(IDC_BTN_NEWPAGE, OnBtnNewpage)
  33.     ON_BN_CLICKED(IDC_BTN_PRINTSETUP, OnBtnPrintsetup)
  34.     ON_BN_CLICKED(IDC_BTN_SETDOCTITLE, OnBtnSetdoctitle)
  35.     //}}AFX_MSG_MAP
  36.     // Standard printing commands
  37.     ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  38.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
  39.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  40. END_MESSAGE_MAP()
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CPreviewPrintView construction/destruction
  44.  
  45. CPreviewPrintView::CPreviewPrintView()
  46.     : CFormView(CPreviewPrintView::IDD)
  47. {
  48.     //{{AFX_DATA_INIT(CPreviewPrintView)
  49.     m_nY = 0;
  50.     m_nX = 0;
  51.     m_strOperations = _T("");
  52.     m_strText = _T("");
  53.     m_nTextAlign = -1;
  54.     m_nHeight = 0;
  55.     m_nOrientation = 0;
  56.     m_nWidth = 0;
  57.     //}}AFX_DATA_INIT
  58.     // TODO: add construction code here
  59.  
  60. }
  61.  
  62. CPreviewPrintView::~CPreviewPrintView()
  63. {
  64. }
  65.  
  66. void CPreviewPrintView::DoDataExchange(CDataExchange* pDX)
  67. {
  68.     CFormView::DoDataExchange(pDX);
  69.     //{{AFX_DATA_MAP(CPreviewPrintView)
  70.     DDX_Control(pDX, IDC_ED_OPS, m_ctrlOperations);
  71.     DDX_Text(pDX, IDC_ED_Y, m_nY);
  72.     DDX_Text(pDX, IDC_ED_X, m_nX);
  73.     DDX_Text(pDX, IDC_ED_OPS, m_strOperations);
  74.     DDX_Text(pDX, IDC_ED_T, m_strText);
  75.     DDX_CBIndex(pDX, IDC_CBO_TEXTALIGN, m_nTextAlign);
  76.     DDX_Text(pDX, IDC_EDT_HEIGHT, m_nHeight);
  77.     DDX_Text(pDX, IDC_EDT_ORIENTATION, m_nOrientation);
  78.     DDX_Text(pDX, IDC_EDT_WIDTH, m_nWidth);
  79.     //}}AFX_DATA_MAP
  80. }
  81.  
  82. BOOL CPreviewPrintView::PreCreateWindow(CREATESTRUCT& cs)
  83. {
  84.     // TODO: Modify the Window class or styles here by modifying
  85.     //  the CREATESTRUCT cs
  86.  
  87.     return CFormView::PreCreateWindow(cs);
  88. }
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CPreviewPrintView printing
  92.  
  93. BOOL CPreviewPrintView::OnPreparePrinting(CPrintInfo* pInfo)
  94. {
  95.     CPreviewPrintDoc *pDoc = GetDocument();
  96.  
  97.     switch (pDoc->GetNumPages())
  98.     {
  99.     case 0:
  100.         // no pages to display or print, so don't bother
  101.         return FALSE;
  102.  
  103.     case 1:
  104.         // if there is only one page, then override the default
  105.         // to only allow a single page to be shown
  106.         pInfo->m_nNumPreviewPages = 1;
  107.     }
  108.  
  109.     // set the page range
  110.     pInfo->SetMinPage(pDoc->GetFirstPage());
  111.     pInfo->SetMaxPage(pDoc->GetLastPage());
  112.     
  113.     // set the current state of preview/print
  114.     GetDocument()->SetPreviewPrintState(
  115.         pInfo->m_bPreview ? 
  116.             CPreviewPrintDoc::PPSTATE_PREVIEW : 
  117.             CPreviewPrintDoc::PPSTATE_PRINT);
  118.  
  119.     // default preparation
  120.     return DoPreparePrinting(pInfo);
  121. }
  122.  
  123. void CPreviewPrintView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* pInfo)
  124. {
  125.     CPreviewPrintApp *pApp = (CPreviewPrintApp *) AfxGetApp();
  126.     AfxGetMainWnd()->SetWindowText(GetDocument()->m_strAppTitle);
  127.     if (pApp->m_bRunOleServer && pInfo->m_bPreview)
  128.     {
  129.         AfxGetMainWnd()->SetWindowPos(&wndTopMost, 0, 0, 0, 0, 
  130.             SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
  131.         AfxGetMainWnd()->SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, 
  132.             SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
  133.         AfxGetMainWnd()->ShowWindow(SW_SHOWMAXIMIZED);
  134.     }
  135. }
  136.  
  137. void CPreviewPrintView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* pInfo)
  138. {
  139.     CPreviewPrintApp *pApp = (CPreviewPrintApp *) AfxGetApp();
  140.     if (pApp->m_bRunOleServer)
  141.     {
  142.         AfxGetMainWnd()->ShowWindow(SW_HIDE);
  143.     }
  144.     GetDocument()->SetPreviewPrintState(CPreviewPrintDoc::PPSTATE_IDLE);
  145. }
  146.  
  147. void CPreviewPrintView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
  148. {
  149.     GetDocument()->PrintPage(pInfo->m_nCurPage, pDC, pInfo);
  150. }
  151.  
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CPreviewPrintView diagnostics
  154.  
  155. #ifdef _DEBUG
  156. void CPreviewPrintView::AssertValid() const
  157. {
  158.     CFormView::AssertValid();
  159. }
  160.  
  161. void CPreviewPrintView::Dump(CDumpContext& dc) const
  162. {
  163.     CFormView::Dump(dc);
  164. }
  165.  
  166. CPreviewPrintDoc* CPreviewPrintView::GetDocument() // non-debug version is inline
  167. {
  168.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPreviewPrintDoc)));
  169.     return (CPreviewPrintDoc*)m_pDocument;
  170. }
  171. #endif //_DEBUG
  172.  
  173. /////////////////////////////////////////////////////////////////////////////
  174. // CPreviewPrintView message handlers
  175.  
  176. void CPreviewPrintView::AddLine(const CString &strLine)
  177. {
  178.     // add the line
  179.     m_strOperations += strLine + "\r\n";
  180.     UpdateData(FALSE);
  181.  
  182.     // scroll so that the last 10 lines are visible
  183.     int nLines = m_ctrlOperations.GetLineCount();
  184.     int nFirst = m_ctrlOperations.GetFirstVisibleLine();
  185.     if (nLines - nFirst > 10)
  186.     {
  187.         m_ctrlOperations.LineScroll(nLines - nFirst - 10);
  188.     }
  189. }
  190.  
  191. void CPreviewPrintView::OnBtnBegindoc() 
  192. {
  193.     CPreviewPrintDoc *pDoc = GetDocument();
  194.     pDoc->BeginDoc();
  195.  
  196.     UpdateData(TRUE);
  197.     m_nOrientation = pDoc->m_nOrientation;
  198.     m_nWidth = pDoc->m_nPageWidth;
  199.     m_nHeight = pDoc->m_nPageHeight;
  200.     UpdateData(FALSE);
  201. }
  202.  
  203. void CPreviewPrintView::OnBtnEnddoc() 
  204. {
  205.     GetDocument()->EndDoc();
  206. }
  207.  
  208. void CPreviewPrintView::OnBtnGetpos() 
  209. {
  210.     UpdateData(TRUE);
  211.     GetDocument()->GetPos(&m_nX, &m_nY);
  212.     UpdateData(FALSE);
  213. }
  214.  
  215. void CPreviewPrintView::OnBtnMoveto() 
  216. {
  217.     UpdateData(TRUE);
  218.     GetDocument()->MoveTo(m_nX, m_nY);
  219. }
  220.  
  221. void CPreviewPrintView::OnBtnLineto() 
  222. {
  223.     UpdateData(TRUE);
  224.     GetDocument()->LineTo(m_nX, m_nY);
  225. }
  226.  
  227. void CPreviewPrintView::OnBtnTextout() 
  228. {
  229.     UpdateData(TRUE);
  230.     GetDocument()->TextOutClip(m_nX, m_nY, m_strText, 
  231.         m_nX, m_nY, m_nX + 100, m_nY + 100);
  232. }
  233.  
  234. void CPreviewPrintView::OnBtnFont() 
  235. {
  236.     CFontDialog fd;
  237.     if (fd.DoModal() == IDOK)
  238.     {
  239.         LOGFONT lf;
  240.         fd.GetCurrentFont(&lf);
  241.  
  242.         // get the options
  243.         short nOptions = 0;
  244.         nOptions |= (lf.lfWeight > 400 ? CPreviewPrintDoc::PPF_BOLD : 0);
  245.         nOptions |= (lf.lfItalic ? CPreviewPrintDoc::PPF_ITALIC : 0);
  246.         nOptions |= (lf.lfUnderline ? CPreviewPrintDoc::PPF_UNDERLINE : 0);
  247.         nOptions |= (lf.lfStrikeOut ? CPreviewPrintDoc::PPF_STRIKEOUT : 0);
  248.  
  249.         // call the setfont function
  250.         GetDocument()->SetFont(lf.lfFaceName, 
  251.             fd.GetSize(), nOptions, 0);
  252.  
  253.         // call the settextcolor function
  254.         GetDocument()->SetTextColor(fd.GetColor());
  255.     }
  256. }
  257.  
  258. void CPreviewPrintView::OnBtnGettextalign() 
  259. {
  260.     UpdateData(TRUE);
  261.     GetDocument()->GetTextAlign();
  262. }
  263.  
  264. void CPreviewPrintView::OnSelchangeCboTextalign() 
  265. {
  266.     UINT uiOptions;
  267.  
  268.     UpdateData(TRUE);
  269.     switch (m_nTextAlign)
  270.     {
  271.     case 0: //TO-L-UPD
  272.     case 1: //TO-L-NO
  273.         uiOptions = TA_TOP | TA_LEFT;
  274.         break;
  275.  
  276.     case 2: //TO-C-UPD
  277.     case 3: //TO-C-NO
  278.         uiOptions = TA_TOP | TA_CENTER;
  279.         break;
  280.  
  281.     case 4: //TO-R-UPD
  282.     case 5: //TO-R-NO
  283.         uiOptions = TA_TOP | TA_RIGHT;
  284.         break;
  285.  
  286.     case 6: //BA-L-UPD
  287.     case 7: //BA-L-NO
  288.         uiOptions = TA_BASELINE | TA_LEFT;
  289.         break;
  290.  
  291.     case 8: //BA-C-UPD
  292.     case 9: //BA-C-NO
  293.         uiOptions = TA_BASELINE | TA_CENTER;
  294.         break;
  295.  
  296.     case 10: //BA-R-UPD
  297.     case 11: //BA-R-NO
  298.         uiOptions = TA_BASELINE | TA_RIGHT;
  299.         break;
  300.  
  301.     case 12: //BO-L-UPD
  302.     case 13: //BO-L-NO
  303.         uiOptions = TA_BOTTOM | TA_LEFT;
  304.         break;
  305.  
  306.     case 14: //BO-C-UPD
  307.     case 15: //BO-C-NO
  308.         uiOptions = TA_BOTTOM | TA_CENTER;
  309.         break;
  310.  
  311.     case 16: //BO-R-UPD
  312.     case 17: //BO-R-NO
  313.         uiOptions = TA_BOTTOM | TA_RIGHT;
  314.         break;
  315.     }
  316.  
  317.     switch (m_nTextAlign)
  318.     {
  319.     case 0: //TO-L-UPD
  320.     case 2: //TO-C-UPD
  321.     case 4: //TO-R-UPD
  322.     case 6: //BA-L-UPD
  323.     case 8: //BA-C-UPD
  324.     case 10: //BA-R-UPD
  325.     case 12: //BO-L-UPD
  326.     case 14: //BO-C-UPD
  327.     case 16: //BO-R-UPD
  328.         uiOptions |= TA_UPDATECP;
  329.         break;
  330.  
  331.     case 1: //TO-L-NO
  332.     case 3: //TO-C-NO
  333.     case 5: //TO-R-NO
  334.     case 7: //BA-L-NO
  335.     case 9: //BA-C-NO
  336.     case 11: //BA-R-NO
  337.     case 13: //BO-L-NO
  338.     case 15: //BO-C-NO
  339.     case 17: //BO-R-NO
  340.         uiOptions |= TA_NOUPDATECP;
  341.         break;
  342.     }
  343.  
  344.     GetDocument()->SetTextAlign(uiOptions);
  345. }
  346.  
  347.  
  348. void CPreviewPrintView::OnBtnNewpage() 
  349. {
  350.     GetDocument()->NewPage();    
  351. }
  352.  
  353. void CPreviewPrintView::OnBtnPrintsetup() 
  354. {
  355.     CPreviewPrintDoc *pDoc = GetDocument();
  356.     pDoc->ShowPrintSetup();
  357.  
  358.     UpdateData(TRUE);
  359.     m_nOrientation = pDoc->m_nOrientation;
  360.     m_nWidth = pDoc->m_nPageWidth;
  361.     m_nHeight = pDoc->m_nPageHeight;
  362.     UpdateData(FALSE);
  363. }
  364.  
  365. void CPreviewPrintView::OnBtnSetdoctitle() 
  366. {
  367.     CPreviewPrintDoc *pDoc = GetDocument();
  368.     pDoc->SetTitle("TestApp", "Test Document");
  369. }
  370.